<?php
namespace app\home\controller;

use think\Controller;
use think\Session;
use think\Request;
use app\home\model\IndexModel;

class Index extends Controller
{
	/**
	*	首页
	*/
	public function index()
	{
		$IndexModel = new IndexModel();
		/* 首页轮播图 */
		$flash = $IndexModel->getFlash();
		/* 首页广告 */
		$adverts = $IndexModel->getAdverts();
		$this->assign('flash',$flash);
		$this->assign('advertsleft',$adverts[0]);
		$this->assign('advertstop',$adverts[1]);
		$this->assign('advertsbuttom',array_slice($adverts,2,3));
		return $this->fetch();
	}

	/**
	*	首页团购商品
	*/
	public function getGroupGoods()
	{
		$IndexModel = new IndexModel();
		$list = $IndexModel->getGroupGoods();
		if($list){
			return returnJson($code=0,$msg='获取成功',$list);
		}else{
			return returnJson($code=1,$msg='获取失败',$list);
		}
	}


	/**
	*	首页最新商品
	*/
	public function getSellGoods()
	{
		$IndexModel = new IndexModel();
		$list = $IndexModel->getSellGoods();
		if($list){
			return returnJson($code=0,$msg='获取成功',$list);
		}else{
			return returnJson($code=1,$msg='获取失败',$list);
		}
	}

	/**
	*	专柜申请页面
	*/
	public function apply()
	{
		return $this->fetch();
	}


	/**
	*	会员专柜申请弹窗提示
	*/
	public function getWelfare()
	{
		$IndexModel = new IndexModel();
		$list = $IndexModel->getWelfare();
		if($list){
			return returnJson($code=0,$msg='获取成功',$list);
		}else{
			return returnJson($code=1,$msg='获取失败',$list);
		}
	}

	/**
	*	会员专柜申请
	*/
	public function addCounters()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$IndexModel = new IndexModel();
			$list = $IndexModel->addCounters($data);
			if($list['success'] == 1){
				// 提交成功
				return returnJson($code=0,$msg=$list['msg'],$list,$url=$list['url']);

			}elseif($list['error'] == 2){
				// 没有登录去登陆
				return returnJson($code=1,$msg=$list['msg'],$list,$url=$list['url']);
			}
		}
	}

	/**
	*	会员申请为合伙人页面
	*/
	public function partner()
	{
		return $this->fetch();
	}

	/**
	*	会员申请为合伙人弹窗提示
	*/
	public function getPartner()
	{
		$IndexModel = new IndexModel();
		$list = $IndexModel->getPartner();
		if($list){
			return returnJson($code=0,$msg='获取成功',$list);
		}else{
			return returnJson($code=1,$msg='获取失败',$list);
		}
	}

	/**
	*	会员申请为合伙人
	*/
	public function addPartner()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$IndexModel = new IndexModel();
			$list = $IndexModel->addPartner($data);
			if($list['success'] == 1){
				// 提交成功
				return returnJson($code=0,$msg=$list['msg'],$list,$url=$list['url']);

			}elseif($list['error'] == 2){
				// 没有登录去登陆
				return returnJson($code=1,$msg=$list['msg'],$list,$url=$list['url']);
			}
		}
	}

	/**
	*	团购倒计时结束隐藏	
	*/
	public function editGoodsStatus()
	{
		$data = $this->request->param();
		$IndexModel = new IndexModel();
		$list = $IndexModel->editGoodsStatus($data['id']);
		if($list){
			return returnJson($code=0,$msg='修改成功',$list);
		}else{
			return returnJson($code=1,$msg='修改失败',$list);
		}
	}


	public function upload(){
    // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file('file');
    
    // 移动到框架应用根目录/public/uploads/ 目录下
    if($file){
        $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
        if($info){
            // 成功上传后 获取上传信息
            $list = $info->getSaveName();
            return returnJson($code=0,$msg='获取成功',$list);
        }else{
            // 上传失败获取错误信息
            $list = $file->getError();
            return returnJson($code=1,$msg='获取失败',$list);
        }
    }
}

}